/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.McKit;

/**
 * Document   : McKitDAO
 * 
 * Created on : 04/03/2013, 09:09:51 
 * 
 * Author     : Fiúza <rbfiuza@gmail.com>
 */ 

public class McKitDAO {
    
    Connection conn = null;
    PreparedStatement stmt = null;
    String sql = null;

    public ArrayList<McKit> getMcKit() {
        ArrayList<McKit> mcKitList = new ArrayList<McKit>();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "mysql");

            sql = "Select"
                    + " id"
                    + ",dsc_mc_kit"
                    + ",qnt_disciplina"
                    + ",qnt_kit"                    
                    + ",qnt_total"
                    + ",vlr_unit"
                    + ",vlr_final"
                    + ",indicacoes_parametros"
                + " from mckit where 1  = ?";

            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, 1);

            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {

                McKit mcKit = new McKit();
                mcKit.setId(rs.getInt("id"));
                mcKit.setDscMcKit(rs.getString("dsc_mc_kit"));
                mcKit.setQntDisciplina(rs.getInt("qnt_disciplina"));
                mcKit.setQntKit(rs.getInt("qnt_kit"));
                mcKit.setQntTotal(rs.getFloat("qnt_total"));
                mcKit.setVlrUnit(rs.getFloat("vlr_unit"));
                mcKit.setVlrFinal(rs.getFloat("vlr_final"));
                mcKit.setIndicacoesParametros(rs.getString("indicacoes_parametros"));
                
                mcKitList.add(mcKit);
            }

            rs.close();
            stmt.close();
            stmt = null;


            conn.close();
            conn = null;

        } catch (Exception e) {
            System.out.println(e);
        } finally {

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here           
                }

                stmt = null;
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here     
                }

                conn = null;
            }
        }

        return mcKitList;
    }

    public String insertMcKit() {
        String userId = null;

        return userId;
    }

    public boolean updateMcKit(String storeId, McKit mcKit) {
        boolean success = true;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "mysql");
            
            sql = "UPDATE mcKit SET "
                    + "id = ?"
                    + ",dsc_mc_kit = ?"
                    + ",qnt_disciplina = ?"
                    + ",qnt_kit = ?"
                    + ",qnt_total = ?"
                    + ",vlr_unit = ?"
                    + ",vlr_final = ?"
                    + ",indicacoes_parametros = ? "
                    + " WHERE id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, mcKit.getId());
            stmt.setString(2, mcKit.getDscMcKit());
            stmt.setInt(3, mcKit.getQntDisciplina());
            stmt.setInt(4, mcKit.getQntKit());
            stmt.setFloat(5, mcKit.getQntTotal());
            stmt.setFloat(6, mcKit.getVlrUnit());
            stmt.setFloat(7, mcKit.getVlrFinal());
            stmt.setString(8, mcKit.getIndicacoesParametros());
            stmt.setInt(9, mcKit.getId());

            stmt.executeUpdate();

            stmt.close();
            stmt = null;
            conn.close();
            conn = null;

        } catch (Exception e) {

            success = false;
            System.out.println(e);

        } finally {

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here           
                }

                stmt = null;
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here     
                }

                conn = null;
            }
        }

        return success;
    }
}